﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.Activities.Presentation.View;
using System.Activities.Presentation.Hosting;
using System.Activities.Presentation.Model;
using System.Windows.Controls;
using System.Activities;
using System.Activities.Presentation;
using MyData.Activities;
using System.Reflection;
using System.ComponentModel.Composition.Hosting;

namespace MyData.Workflow.Designer
{
  public class MyEditorService : IExpressionEditorService
  {
    public MyExpressionEditor instance = null;

    public void CloseExpressionEditors(){}

    public IExpressionEditorInstance CreateExpressionEditor(AssemblyContextControlItem assemblies, ImportedNamespaceContextItem importedNamespaces, List<ModelItem> variables, string text)
    {
      instance = new MyExpressionEditor();
      instance.Text = text;
      return instance;
    }

    //Called to get
    //  ModelProperty mpTo = mi.Properties["To"];
    //  ModelProperty mpExpression = mpT.Value.Properties["Expression"].Value.Properties["ExpressionText"];
    public IExpressionEditorInstance CreateExpressionEditor(AssemblyContextControlItem assemblies, ImportedNamespaceContextItem importedNamespaces, List<ModelItem> variables, string text, System.Windows.Size initialSize)
    {
      //Hook into Assign activity
      //NEXT: Change path
      var catalog = new AggregateCatalog(new DirectoryCatalog(MyData.Workflow.Designer.Properties.Settings.Default.ExtensionsPath), new AssemblyCatalog(Assembly.GetExecutingAssembly()));
      var container = new CompositionContainer(catalog);

      instance = container.GetExportedValue<MyExpressionEditor>();

      instance.Variables = variables;
      instance.Text = text;
      return instance;
    }

    public IExpressionEditorInstance CreateExpressionEditor(AssemblyContextControlItem assemblies, ImportedNamespaceContextItem importedNamespaces, List<ModelItem> variables, string text, Type expressionType)
    {
      instance = new MyExpressionEditor();
      instance.Text = text;
      return instance;
    }


    //"Value"
    //Called to get
    //  ModelProperty mpValue = mi.Properties["Value"];
    //  ModelProperty mpExpression = mpT.Value.Properties["Expression"].Value.Properties["ExpressionText"];
    public IExpressionEditorInstance CreateExpressionEditor(AssemblyContextControlItem assemblies, ImportedNamespaceContextItem importedNamespaces, List<ModelItem> variables, string text, Type expressionType, System.Windows.Size initialSize)
    {
      //Resolve MEF Builders
      var catalog = new AggregateCatalog(new DirectoryCatalog(MyData.Workflow.Designer.Properties.Settings.Default.ExtensionsPath), new AssemblyCatalog(Assembly.GetExecutingAssembly()));
      var container = new CompositionContainer(catalog);

      instance = container.GetExportedValue<MyExpressionEditor>();

      instance.Text = text;
      instance.Variables = variables;
      instance.ExpressionType = expressionType;


      return instance;
    }

    public void UpdateContext(AssemblyContextControlItem assemblies, ImportedNamespaceContextItem importedNamespaces){}
  }
}